import { TestService } from "@/shared/api/api.test";
import { IQuestion } from "@/shared/interfaces";
import { QueryClient, useQuery } from "@tanstack/react-query";
import { AxiosError } from "axios";



export function useGetQuestions(testId: number) {
    const queryClient = new QueryClient


    const getQuestions = useQuery<IQuestion[], AxiosError<{ message: string }>>(
        {
            queryKey: ['questions', testId],
            queryFn: () => TestService.getQuestions(testId),
            retry: 3,
            placeholderData: (previousData) => previousData,
        }, queryClient
    );

    const errorMessage = getQuestions.error?.response?.data.message

    return {
        isSuccess: getQuestions.isSuccess,
        isPending: getQuestions.isPending,
        refetch: getQuestions.refetch,
        errorMessage,
        questions: getQuestions.data
    };
}